package com.xiaoyu.client.ui.activity.main.mine.set_favorable;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xiaoyu.client.R;
import com.xiaoyu.client.ui.activity.main.mine.set_favorable.AvailableFragment;
import com.xiaoyu.client.ui.activity.mall.OrderComfirmActivity;
import com.xiaoyu.commonlib.ui.base.BaseActivityWithTitle;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author jx
 * @date 创建时间 2018/10/15
 * @Description 我的优惠卷
 */
public class FavorableActivity extends BaseActivityWithTitle {

    @BindView(R.id.activity_favorable_available)
    TextView mAvailable;//可用优惠卷
    @BindView(R.id.activity_favorable_have_been_used)
    TextView mHaveBeenUsed;//已使用
    @BindView(R.id.activity_favorable_have_expired)
    TextView mHaveExpired;//已过期

    private List<Fragment> mFragments = new ArrayList<Fragment>();

    @Override
    protected void loadContentView(ViewGroup parent) {
        setTitleText("我的优惠卷");
        View mView = LayoutInflater.from(this).inflate(R.layout.activity_favorable, parent, true);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mAvailable.setOnClickListener(this);
        mHaveBeenUsed.setOnClickListener(this);
        mHaveExpired.setOnClickListener(this);
        PrepareFragment();
    }

    //准备fragment
    private void PrepareFragment() {
        AvailableFragment availableFragment = new AvailableFragment(1);
        if (getIntent() != null){
            Bundle bundle = new Bundle();
            bundle.putDouble(OrderComfirmActivity.ORDER_MONEY,getIntent().getDoubleExtra(OrderComfirmActivity.ORDER_MONEY,-0.01));
            availableFragment.setArguments(bundle);
        }
        mFragments.add(availableFragment);
        mFragments.add(new AvailableFragment(2));
        mFragments.add(new AvailableFragment(3));
        showFragment(0);
    }

    private void showFragment(int i) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.activity_favorable_framelayout, mFragments.get(i));
        transaction.commit();
        switch (i) {
            case 0:
                mAvailable.setTextColor(Color.parseColor("#ffffff"));
                mAvailable.setBackgroundColor(Color.parseColor("#2796f0"));

                mHaveBeenUsed.setBackgroundColor(Color.parseColor("#DCDCDC"));
                mHaveBeenUsed.setTextColor(Color.parseColor("#333333"));

                mHaveExpired.setBackgroundColor(Color.parseColor("#DCDCDC"));
                mHaveExpired.setTextColor(Color.parseColor("#333333"));

                break;
            case 1:
                mAvailable.setTextColor(Color.parseColor("#333333"));
                mAvailable.setBackgroundColor(Color.parseColor("#DCDCDC"));

                mHaveBeenUsed.setTextColor(Color.parseColor("#ffffff"));
                mHaveBeenUsed.setBackgroundColor(Color.parseColor("#2796f0"));

                mHaveExpired.setTextColor(Color.parseColor("#333333"));
                mHaveExpired.setBackgroundColor(Color.parseColor("#DCDCDC"));
                break;
            case 2:
                mAvailable.setTextColor(Color.parseColor("#333333"));
                mAvailable.setBackgroundColor(Color.parseColor("#DCDCDC"));

                mHaveBeenUsed.setBackgroundColor(Color.parseColor("#DCDCDC"));
                mHaveBeenUsed.setTextColor(Color.parseColor("#333333"));

                mHaveExpired.setBackgroundColor(Color.parseColor("#2796f0"));
                mHaveExpired.setTextColor(Color.parseColor("#ffffff"));

        }
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == mAvailable) {//可用
            showFragment(0);
        }
        if (v == mHaveBeenUsed) {//已使用
            showFragment(1);
        }
        if (v == mHaveExpired) {//已过期
            showFragment(2);
        }
    }
}
